import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class SvrThread implements Runnable {

	private Socket incoming;
	private OutputStream outStream;
	private InputStream inStream;
	private PrintWriter out;
	private Scanner in;

	public SvrThread(Socket incoming) {
		this.incoming = incoming;
		try {
			inStream = incoming.getInputStream();
			outStream = incoming.getOutputStream();
			out = new PrintWriter(outStream, true);
			in = new Scanner(inStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {

			parseCmd();
		} finally {
			// System.out.println("koniec transmisji");
			try {
				incoming.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public synchronized void parseCmd() {

		String cmd = new String();
		String tokens[] = new String[3];
		for (String t : tokens)
			t = new String();
		while (in.hasNextLine()) {
			cmd = in.nextLine();
			tokens = cmd.split(";");
			// System.out.println(cmd);
			// System.out.println(tokens[0] + " " + tokens[1] + " " +
			// tokens[2]);
			if (tokens[0].compareTo("exit") == 0) {
				break;
			}

			if (tokens[0].compareTo("download") == 0) {
				sendFile(tokens[1], tokens[2]);
				break;
			}
		}

		/*
		 * } catch (InterruptedException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */

	}

	private void sendFile(String filename, String size) {
		// znajdz plik, posiekaj, wyslij na incoming
		File fileToSend = new File(filename);
		// fileToSend.mkdirs();
		// fileToSend.delete();
		// byte part[] = new byte[1024];
		int last = 0, flag = 0;
		boolean ack = false;

		try {
			FileInputStream fileOut = new FileInputStream(fileToSend);

			// OutputFileStream fileOut = new OutputFileStream(fileToSend);
			long packetCount = 0;
			if (fileToSend.length() == Integer.parseInt(size)) {
				if (fileToSend.length() % 1024 == 0) {
					packetCount = fileToSend.length() / 1024;
					last = 1024;
				} else {
					packetCount = fileToSend.length() / 1024 + 1;
					last = (int) (fileToSend.length() - (packetCount - 1) * 1024);
				}

				byte part[] = new byte[(int) fileToSend.length()];
				System.out.println(fileOut.read(part));
				System.out.println("packets;" + packetCount);
				out.println("packets;" + packetCount);
				out.println("start");

				for (int i = 0; i < packetCount; i++) {
					System.out.println("wysylam");
					ack = false;
					/*if(i == packetCount-1){
						System.out.println("last: "+last);
						part = new byte[last];
					}*/
					while (!ack) {
						if (i == packetCount - 1)
							outStream.write(part, i * 1024, last);
						else outStream.write(part, i*1024, 1024);
						
						outStream.flush();
						String str = in.nextLine();
						System.out.println("str: " + str);
						if (str.compareTo("ack" + i) == 0)
							ack = true;
						else if (str.compareTo("lost" + i) == 0) {
							ack = false;
						}
						System.out.println(ack);
					}
					// Thread.sleep(1);
				}
				outStream.flush();
				outStream.close();
				out.close();
				fileOut.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}/*
		 * catch (InterruptedException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
	}
}
